package com.experienceBaby.Cashbook.fragment;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.experienceBaby.Cashbook.R;
import com.experienceBaby.Cashbook.databinding.FragmentLoginBinding;

import org.jetbrains.annotations.NotNull;

import java.util.Timer;
import java.util.TimerTask;

public class LoginFragment extends Fragment implements TextWatcher {

    public static String USER_NAME = "admin";
    FragmentLoginBinding binding;
    UserFragment userFragment;

    public LoginFragment(UserFragment userFragment) {
        this.userFragment = userFragment;
    }

    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        binding = FragmentLoginBinding.inflate(inflater, container, false);
        initData();
        initView();
        return binding.getRoot();
    }

    private void initView() {
        binding.etPassword.addTextChangedListener(this);
        binding.etUser.addTextChangedListener(this);
        binding.btLogin.setOnClickListener(view -> {
            String user = binding.etUser.getText().toString();
            String pwd = binding.etPassword.getText().toString();
            if (user.equals(USER_NAME) && pwd.equals("admin")) {
                ProgressDialog dialog = new ProgressDialog(getContext());
                dialog.setTitle("登录中...");
                dialog.show();
                TimerTask timerTask = new TimerTask() {
                    @Override
                    public void run() {
                        getActivity().getSharedPreferences("User", Context.MODE_PRIVATE).edit().putBoolean("isLogin", true).apply();
                        dialog.dismiss();
                        userFragment.login();
                    }
                };
                Timer timer = new Timer();
                timer.schedule(timerTask, 1000);
            } else {
                showToast("账号或密码错误");
            }
        });
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    private void showToast(String text) {
        Toast.makeText(getContext(), text, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void afterTextChanged(Editable s) {
        //判断两个输入框是否有内容
        if (binding.btLogin.getText().toString().length() > 0 &&
                binding.etPassword.getText().toString().length() > 0) {
            //按钮可以点击
            binding.btLogin.setEnabled(true);
        } else {
            //按钮不能点击
            binding.btLogin.setEnabled(false);
        }
        if (binding.etUser.getText().toString().equals(USER_NAME)) {
            Glide
                    .with(getContext())
                    .load(R.drawable.icon)
                    .into(binding.userIcon);
        } else {
            Glide
                    .with(getContext())
                    .load(R.drawable.user_icon)
                    .into(binding.userIcon);
        }
    }

    private void initData() {

    }
}
